import { reactive, onMounted, onBeforeUnmount, getCurrentInstance } from 'vue';
import page from './page.ts';
import { useRouter, useRoute } from 'vue-router';
export function useListComponent() {
  const router = useRouter();
  const route = useRoute();
  const { proxy } = getCurrentInstance() as any;
  //页面信息
  const dataInfo: any = reactive({
    loading: false, //加载
    loadingMsg: '列表数据加载中', //加载
    table: {
      title: page.title, //表格-标题
      //表格-内容数据
      tbody: [],
      rowKey: 'dataBillNo',
      theadCode: 'dataproducebilllist', //basedatatable
      //表格-表头数据
      thead: [],
      //表格-搜索数据
      search: {
        pageIndex: 1, //当前页码
        pageSize: 30, //每页显示数据的数量
        totalCount: 0, //总数量
        queryDataBaseFiled: 'xieYiShuHao', // 关键字查询字段，空值表示查询全部
        likeWords: '', // 查询关键字，空值表示查询全部
        beginTime: '', // 日期范围-开始时间
        endTime: '', // 日期范围-结束时间
        onlySelf: 0, // 是否只查自己创建的：0 查所有；1 只查自己
        isShowDisabled: -1, // 是否显示禁用：-1 所有；0 未作废；1 仅作废
        orderByFiled: '', // 排序字段
        orderByType: 0, // 排序方式：0 倒序；1 升序
        dataBillState: -1, // 数据生产单状态：0未到达；1制作中；2已完成；-1查询全部
      },
      //多选
      checkbox: {
        state: true,
        selectable: (row: any) => {
          return true;
        },
        list: [],
      },
      //表格-按钮数据
      btns: [
        {
          title: '导出数据',
          icon: 'Download',
          type: '',
          children: [
            {
              title: '导出当页数据',
              click: () => {
                setLoading('导出当页数据,请稍等');
                exportBtn(dataInfo.table.tbody);
              },
            },
            {
              title: '导出全部数据',
              click: () => {
                setLoading('正在导出全部数据,请稍等');
                proxy.$api.GetPageDataProduceBillDataList.GetPageDataProduceBillDataList({
                  ...dataInfo.table.search,
                  pageIndex: 1,
                  pageSize: 10000,
                  beginTime: dataInfo.table.search.beginTime || proxy.$getTimeAgo(1, 'years'), // 开始时间
                  endTime: dataInfo.table.search.endTime || proxy.$day(new Date()), // 结束时间
                })
                  .then((res: any) => {
                    exportBtn(res.list);
                  })
                  .catch(() => {
                    dataInfo.loading = false;
                  });
              },
            },
            {
              title: '导出选中数据',
              click: () => {
                setLoading('导出选中数据,请稍等');
                exportBtn(dataInfo.table.checkbox.list);
              },
            },
          ],
        },
      ],
      //表格-列表搜索
      operation: [
        {
          title: '编辑',
          click: (row: any) => {
            editBtn(row, 2);
          },
        },
        {
          title: '删除',
          disabled: (row: any) => {
            return ![0].includes(row.dataBillState);
          },
          click: (row: any) => {
            proxy.$messageBox('是否删除当前数据，请确认！', () => {
              proxy.$api.ProductionBusniess.DeleteDataProduceBill({
                dataBillID: row.dataBillID,
              })
                .then((res: any) => {
                  proxy.$message('删除成功');
                  getTbody();
                })
                .catch(() => {});
            });
          },
        },
      ],
    },
    tableAdd: {
      tbody: [],
      thead: [
        // {
        //   label: '订单单号',
        //   prop: 'saleOrderNo',
        //   align: 'center',
        //   width: 140,
        // },
        {
          label: '协议书号',
          prop: 'xieYiShuHao',
          align: 'center',
          width: 140,
          fixed: 'left',
        },
        {
          label: '生产单号',
          prop: 'produceTaskBillNo',
          align: 'center',
          width: 140,
          fixed: 'left',
        },
        {
          label: '订单类型',
          prop: 'orderTypeName',
          align: 'center',
          width: 80,
        },
        {
          label: '交货日期',
          prop: 'expectedDeliveryTime',
          align: 'center',
          width: 120,
        },
        {
          label: '产品名称',
          prop: 'orderProductName',
          align: 'center',
          width: null,
        },
        {
          label: '产品版本',
          prop: 'productVersions',
          align: 'center',
          width: 80,
        },
        {
          label: '印刷版面',
          prop: 'printingSurface',
          align: 'center',
          width: 80,
        },
        {
          label: '客户名称',
          prop: 'customerName',
          align: 'center',
          width: 220,
        },
        {
          label: '开单日期',
          prop: 'orderCreateTime',
          align: 'center',
          width: 120,
        },
        {
          label: '业务人员',
          prop: 'orderCreatePerson',
          align: 'center',
          width: 100,
        },
      ],
      getTbody: (table: any) => {
        proxy.$api.OrderBusniess.GetCanOpenDataProduceBillsSaleOrderList({
          likeWords: '',
        })
          .then((res: any) => {
            table.tbody = res;
          })
          .catch(() => {});
      },
      selectBtn: (row: any) => {
        if (row.produceTaskBillNo) {
          getPath({
            path: `${route.path}/details`,
            query: {
              type: 1, //页面类型
              produceTaskBillNo: row.produceTaskBillNo, //生产单号
            },
          });
        } else {
          proxy.$message('未找到对应的生产单号', 'error');
        }
      },
    },
  });
  const options: any = reactive({
    state: [
      {
        label: '未到达',
        value: 0,
        type: 'info',
      },
      {
        label: '制作中',
        value: 1,
        type: 'info',
      },
      {
        label: '已完成',
        value: 3,
        type: 'success',
      },
    ],
    stateName: [
      {
        label: '未到达',
        value: '未到达',
        type: 'info',
      },
      {
        label: '制作中',
        value: '制作中',
        type: '',
      },
      {
        label: '已到达',
        value: '已到达',
        type: 'warning',
      },
      {
        label: '已完成',
        value: '已完成',
        type: 'success',
      },
    ],
    dataColor: [], //获取数据颜色
    dataFormat: [], //获取数据格式
    inspectProcess: [], //获取自检数据
  });
  //编辑
  const editBtn = (row: any, type: number = 3) => {
    getPath({
      path: `${route.path}/details`,
      query: {
        type: type, //页面类型
        produceTaskBillNo: row.produceTaskBillNo, //生产单号
        dataBillID: row.dataBillID, //数据生产单主键ID
        dataBillNo: row.dataBillNo, //数据生产单号
      },
    });
  };
  // 获取tabs数据
  const getPath = (obj: any, row?: any) => {
    const path = router.resolve(obj);
    // 在新窗口或标签页中打开目标路径
    const newWindow: any = window.open(path.href, '_blank');
    newWindow;
  };
  // 获取表数据
  const getTbody = () => {
    let _table: any = dataInfo.table;
    setLoading();
    proxy.$api.ProductionBusniess.GetPageDataProduceBillDataList({
      ..._table.search,
    })
      .then((res: any) => {
        _table.tbody = res.list;
        _table.search.totalCount = res.totalCount;
        dataInfo.loading = false;
      })
      .catch(() => {
        dataInfo.loading = false;
      });
    let _tableAdd: any = dataInfo.tableAdd;
    if (_tableAdd && proxy.$isFunction(_tableAdd.getTbody)) {
      _tableAdd.getTbody(_tableAdd);
    }
  };
  // 导出数据
  const exportBtn = (tbody: any[], thead?: any[]) => {
    proxy.$exportData(
      {
        thead: thead || dataInfo.table.thead,
        tbody: tbody,
        fileName: dataInfo.table.title,
      },
      dataInfo,
    );
    setTimeout(() => {
      dataInfo.loading = false;
    }, 1000);
  };
  const setLoading = (title: string = '正在获取数据,请稍等...') => {
    if (!dataInfo.loading) {
      dataInfo.loading = true;
      dataInfo.loadingMsg = title;
    }
  };
  // 组件挂载后执行
  onMounted(() => {
    getTbody();
  });
  // 组件卸载前执行
  onBeforeUnmount(() => {});
  return { dataInfo, getTbody, options };
}
